package furns.dao.impl;

import furns.dao.BasicDAO;
import furns.dao.MemberDAO;
import furns.entity.Member;

//DAO层的实现类对象负责实现相关方法，通过调用basicDAO的方法实现对数据库的CRUD操作
public class MemberDAOImpl extends BasicDAO<Member> implements MemberDAO {
    /**
     * 根据用户名查询用户
     * @param username 用户名
     * @return   若存在该对象返回用户信息，否则返回null
     */
    @Override
    public Member queryMemberByUsername(String username) {
        String sql = "SELECT `id`,`username`,`password`,`email` FROM `member`\n" +
                " WHERE `username`= ?";
        return querySingle(sql, Member.class, username);
    }

    /**
     *保存用户
     * @param member 用户名
     * @return 返回-1表示保存失败，返回其他数字（数据表受影响的行数）表示保存成功
     */
    @Override
    public int saveMember(Member member) {
        String sql = "INSERT INTO `member`(`username`,`password`,`email`) " +
                " VALUES(?,MD5(?), ?)";
        return update(sql, member.getUsername(),
                member.getPassword(), member.getEmail());
    }

    /**
     *
     * @param username 用户名
     * @param password 密码
     * @return 若用户存在则返回该对象，若不存在则返回null
     */
    @Override
    public Member queryMemberByUsernameAndPassword(String username, String password) {
        String sql = "SELECT `id`,`username`,`password`,`email` FROM `member`\n" +
                " WHERE `username`= ? and `password`= MD5(?)";
        return querySingle(sql, Member.class, username, password);
    }
}
